package gameheart.client.logic;

import gameheart.client.sprite.CardSprite;
import gameheart.client.sprite.ImageSprite;
import gameheart.client.sprite.Sprite;
import gameheart.transport.MyException;
import gameheart.server.ServerCard;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PassingCardState extends PlayState {

    private ArrayList players[] = new ArrayList[4];
    private Sprite _passButton;

    public PassingCardState(PlayCanvas context) {
        super(context);
        for (int index = 0; index < players.length; ++index) {
            players[index] = new ArrayList();
        }
    }

    @Override
    public void render(Graphics graphic) {
        for (int playerIndex = 0; playerIndex < players.length; ++playerIndex) {
            for (int cardIndex = 0; cardIndex < players[playerIndex].size(); ++cardIndex) {
                CardSprite sprite = (CardSprite) players[playerIndex].get(cardIndex);
                sprite.draw(graphic);
            }
        }
        if (_passButton != null) {
            _passButton.draw(graphic);
        }
    }

    @Override
    public PlayState Clone() {
        return new PassingCardState(_context);
    }

    @Override
    public playStateType getStateType() {
        return playStateType.PassingCardState;
    }
    ArrayList<CardSprite> _selectedCard = new ArrayList<CardSprite>();

    @Override
    public void update() {
        if (_context._clickedPosition.getRect() != null) {
            Rectangle rect = _context._clickedPosition.getRect();
            for (int cardIndex = players[0].size() - 1; cardIndex >= 0; --cardIndex) {
                CardSprite sprite = (CardSprite) players[0].get(cardIndex);
                if (sprite.isIntersec(rect)) {
                    if (_selectedCard.contains(sprite)) {
                        _selectedCard.remove(sprite);
                        sprite.setY(sprite.getY() + 30);
                    } else {
                        if (_selectedCard.size() < 3) {
                            sprite.setY(sprite.getY() - 30);
                            _selectedCard.add(sprite);
                        }

                    }
                    break;
                }
            }
            if (_passButton != null && _passButton.isIntersec(rect)) {
                if (_selectedCard.size() == 3) {
                    ServerCard cards[] = new ServerCard[3];
                    for (int index = 0; index < 3; ++index) {
                        cards[index] = _selectedCard.get(index)._content;
                    }
                    try {
                        _context._mainGame.getAdapter().passCard(cards);
                        _passButton = null;
                    } catch (MyException ex) {
                        Logger.getLogger(PassingCardState.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
            _context._clickedPosition.setRect(null);
        }
        if (_context._mainGame.getAdapter().passCardDone()) {
            _context.changeState(playStateType.HandState);
        }
    }

    @Override
    public void Init() {
        Point delta = _context._deltaVector[0];
        for (int index = 0; index < 13; ++index) {
            Point beginLocation = _context._beginLocationOfPlayers[0];
            int x = beginLocation.x + delta.x * index;
            int y = beginLocation.y + delta.y * index;

            ServerCard[] myCards = _context._mainGame.getAdapter().getMyCards();
            players[0].add(new CardSprite(x, y, myCards[index], _context._imageResposity));
        }
        for (int playerIndex = 1; playerIndex < players.length; ++playerIndex) {
            delta = _context._deltaVector[playerIndex];
            for (int cardIndex = 0; cardIndex < 13; ++cardIndex) {
                Point beginLocation = _context._beginLocationOfPlayers[playerIndex];
                int x = beginLocation.x + delta.x * cardIndex;
                int y = beginLocation.y + delta.y * cardIndex;


                players[playerIndex].add(new CardSprite(x, y, null, _context._imageResposity));
            }
        }
        String name = _context._mainGame.getAdapter().getPassCardDirection().toString() + ".png";
        _passButton = new ImageSprite(400, 400, _context._imageResposity, name);
        _context.notify("let's pass card");
    }
}
